package com.ss.android.article.base.feature.f.a;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.video.GetPlayUrlThread;
import com.ss.android.article.base.feature.video.MediaHelper;
import com.ss.android.article.browser.R;
import com.ss.android.newmedia.BaseApplication;
import com.ss.ttvideoengine.b.VideoDataContainer;
import com.ss.ttvideoengine.b.VideoInfo;

import org.json.JSONObject;

public abstract class b extends com.ss.android.article.base.feature.f.a.f {
    private int a;
    public long b;
    protected String c;
    public JSONObject d;
    Handler e;
    int f;
    Runnable g;
    private int r;
    private int s;
    private int t;
    private VideoDataContainer u;
    private GetPlayUrlThread v;

    public b(Context arg2) {
        super(arg2);
        this.a = -981;
        this.r = -980;
        this.s = -979;
        this.t = -978;
        this.f = 0;
        this.g = new com.ss.android.article.base.feature.f.a.e(this);
        this.d = new JSONObject();
        this.e = new Handler();
    }

    static  com.ss.android.m.c.aa a(b arg1) {
        return arg1.j;
    }

    public final com.ss.android.m.c.aa a(com.ss.android.m.c.a.a arg2, Handler arg3) {
        return new com.ss.android.article.base.feature.f.b.a(arg2, arg3);
    }

    public void a(int arg14, String arg15, long arg16, String arg18, int arg19, long arg20) {
        this.b = arg20;
        this.c = arg15;
        this.v = new GetPlayUrlThread(new WeakHandler(new c(this)), arg14, arg15, arg16, arg18, arg19, arg20, true);
        this.v.start();
    }

    protected void a(Bundle arg1) {
    }

    protected void a(Bundle arg3, Object arg4) {
        if(!android.support.design.a.isTextEmpty(arg3.getString("video_id")) && ((arg4 instanceof VideoDataContainer))) {
            this.u = ((VideoDataContainer)arg4);
            VideoInfo v0 = this.u.mVideoInfo;
            String v0_1 = v0 == null || v0.mVideoItem1 == null ? null : MediaHelper.getVideoSource(v0.mVideoItem1.mainUrl);
            this.a(v0_1, true);
        }
    }

    public void a(String arg4, boolean arg5) {
        if(!TextUtils.isEmpty(((CharSequence)arg4))) {
            com.ss.android.article.base.feature.f.c.a v0 = this.h;
            com.ss.android.article.base.feature.f.a.d v1 = new com.ss.android.article.base.feature.f.a.d(this, arg4);
            if(v0.f) {
                ((Runnable)v1).run();
            }
            else {
                v0.g = ((Runnable)v1);
            }
        }
    }

    final void a(Message arg3) {
        if(!this.m() && arg3 != null) {
            switch(arg3.what) {
                case 10: {
                    Bundle v0 = arg3.getData();
                    this.a(v0);
                    this.a(v0, arg3.obj);
                    return;
                }
                case 11: {
                    this.b(arg3.getData());
                    this.c(11, -1);
                    return;
                }
            }

            return;


        }
    }

    static com.ss.android.m.c.aa b(b arg1) {
        return arg1.j;
    }

    protected void b(Bundle arg6) {
        int v4 = R.string.video_fetch_url_fail;
        String v0 = arg6.getString("error1");
        String v1 = arg6.getString("error2");
        Context v2 = this.n();
        if(("time out".equals(v0)) || ("time out".equals(v1))) {
            UIUtils.showImageToast(v2, v2.getString(v4) + this.a);
        }
        else {
            if(!"api fail".equals(v0) && !"api fail".equals(v1)) {
                if(!"empty response".equals(v0) && !"empty response".equals(v1)) {
                    UIUtils.showImageToast(v2, v2.getString(v4) + this.t);
                    return;
                }

                UIUtils.showImageToast(v2, v2.getString(v4) + this.s);
                return;
            }

            UIUtils.showImageToast(v2, v2.getString(v4) + this.r);
        }
    }

    public void b() {
        super.b();
        ((com.ss.android.article.base.feature.f.b.a)this.j).b = true;
    }

    private void c() {
        if(this.v != null) {
            this.v.cancelRequest();
        }

        if(this.e != null) {
            this.e.removeCallbacks(this.g);
        }
    }

    public void c(int arg1, int arg2) {
        super.c(arg1, arg2);
        this.c();
    }

    public void d() {
        super.d();
        this.c();
    }

    public void e() {
        super.e();
        this.c();
    }

    protected String f() {
        String v0;
        if(this.u == null || this.u.mVideoInfo == null || this.u.mVideoInfo.mVideoItem1 == null) {
            v0 = null;
        }
        else {
            v0 = this.u.mVideoInfo.mVideoItem1.backupUrl1;
            if(this.f == 1) {
                v0 = this.u.mVideoInfo.mVideoItem1.backupUrl2;
            }
            else if(this.f == 2) {
                v0 = this.u.mVideoInfo.mVideoItem1.backupUrl3;
            }

            v0 = MediaHelper.getVideoSource(v0);
            if(TextUtils.isEmpty(((CharSequence)v0))) {
                return v0;
            }

            com.ss.android.article.base.feature.f.e.k();
            this.e();
            this.a(v0, true);
        }

        return v0;
    }

    public void g() {
        super.g();
        this.c();
        ((com.ss.android.article.base.feature.f.b.a)this.j).a = true;
    }

    protected void h() {
        this.g();
    }

    public final void i() {
        super.i();
        this.a(true);
    }

    public final void j() {
        super.j();
        if(this.e != null) {
            this.e.removeCallbacks(this.g);
        }
    }

    protected static long k() {
        return ((long)(AppData.getInstance().mVideoPlayRetryInterval * 1000));
    }

    static void l() {
        if(BaseApplication.getApplication() != null) {
            ((AudioManager) BaseApplication.getApplication().getSystemService(Context.AUDIO_SERVICE)).requestAudioFocus(null, 3, 1);
        }
    }
}

